home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 28 (1992-05)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 28 (1992-05)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Basic_Booleans / Basic_Booleans
Text File  |  1992-05-26  |  5KB  |  171 lines

  1.  
  2.  
  3.                            B O O L E A N   B A S I C S  
  4.  
  5.                                 by Paul McLachlan
  6.  
  7.  
  8.  
  9.    Ed: Here's a fine tutorial on a little-used aspect of programming in
  10.   Basic, and which can add a lot of elegance and speed to your programs.
  11.  
  12.  
  13.    ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 |||
  14.  
  15.  
  16.    Booleans are facinating tools for programming in BASIC. They allow you to
  17.   get the computer to do more, with fewer lines. They are also considerably
  18.   faster. Unfortunately, these are little spoken of features of BASIC, and
  19.   you see nothing of them in the AmigaBASIC manual supplied with your
  20.   computer. They are useful in only a small variety of circumstances, but
  21.   when they do come in handy, you'll be glad you knew about them. Please
  22.   note that before we start this, all boolean statements can be replaced by
  23.   conventional BASIC statements (eg IF-THEN-ELSE). It just requires a few
  24.   more lines.
  25.  
  26.    A BOOLEAN EXPRESSION is a way of asking the computer whether something is
  27.   true, or false. If the expression is true, AmigaBASIC returns a -1. If
  28.   the expression is false, AmigaBASIC returns a 0. Consider the statement:
  29.  
  30. PRINT a=b
  31.  
  32.    Now, we all know that this will not output an 'a', followed by an equal
  33.   sign, followed by a 'b', because it is not enclosed in double quotes. (").
  34.   We also know it cannot output the value of the variable 'a=b', because
  35.   variables do not have equals signs in them. So, what does it do? It
  36.   evaluates the expression 'a=b' and decides if it is true, or false. If it
  37.   is true, ie, if a does in fact equal b, then the Amiga will print a -1 on
  38.   the screen. If it is false, the Amiga will print a 0. In the same way,
  39.   the following works.
  40.  
  41. c=a=b
  42.  
  43.    This makes 'c' equal to the outcome of the test, 'a=b'. If a is equal to
  44.   b, then c becomes equal to -1. If a is not equal to b, then c becomes
  45.   equal to 0.
  46.  
  47.    Boolean expressions must be enclosed in brackets when there are more than
  48.   one together. Brackets are also used to increase readibility with one
  49.   boolean expression. Take the first example, and compare it with the second
  50.   example.
  51.  
  52. c=a=b
  53. c=(a=b)
  54.  
  55.    Obviously the second example is easier to read, and it makes it easier to
  56.   debug. Longer boolean expressions must be enclosed in brackets. Eg
  57.  
  58. a=(b=(c=(d=5)))
  59.  
  60.    Keep in mind that for a to be equal to -1, the following conditions must
  61.   be met.
  62.  
  63.     if d is equal to 5:
  64.       if c is equal to -1:
  65.         b must be equal to -1
  66.       if c is equal to 0:
  67.         b must be equal to 0:
  68.     if, however, d is not equal to 5
  69.       if c is equal to -1:
  70.         b must be equal to 0
  71.       if c is equal to 0:
  72.         b must be equal to -1
  73.  
  74.    So, we see, this is quite an impressive list of options. To do the same
  75.   with conventional BASIC requires this: (assuming all variables have been
  76.   set before)
  77.  
  78.     REM SIMULATING THE BOOLEAN STATEMENT A=(B=(C=(D=5)))
  79.     IF D=5 THEN
  80.       IF C=-1 THEN
  81.         IF B=-1 THEN
  82.           LET A=-1
  83.         ELSE
  84.           LET A=0
  85.         END IF
  86.       ELSE
  87.         IF B=0 THEN
  88.           LET A=0
  89.         ELSE
  90.           LET A=-1
  91.         END IF
  92.       END IF
  93.     ELSE
  94.       IF C=-1 THEN
  95.         IF B=-1 THEN
  96.           LET A=0
  97.         ELSE
  98.           LET A=-1
  99.         END IF
  100.       ELSE
  101.         IF B=0 THEN
  102.           LET A=-1
  103.         ELSE
  104.           LET A=0
  105.         END IF
  106.       END IF
  107.     END IF
  108.  
  109.  
  110.    So, it appears that booleans just might come in handy one day, doesn't it?
  111.  
  112.    Boolean expressions are not just limited to ='s. <'s,>'s, AND's, OR's,
  113.   and everything that you might usually use in an IF statement are allowed.
  114.   Be forewarned, however - don't over-do it. Some boolean expressions can be
  115.   slower than the IF-THEN equivalent. Not many of them are like this,
  116.   especially not the example above - most are quicker. However, all speed
  117.   hints when using AmigaBASIC should be passed around, so I'm just warning
  118.   you.
  119.  
  120.    I'm not sure whether ALL types of BASIC on the Amiga use -1 to return a
  121.   true. I know that the Apple II Microsoft BASIC returns a 1, instead of a
  122.   -1. Some commercial BASIC's may use 1's also. It's easy to find out.
  123.   Just type
  124.  
  125.   PRINT 1=1
  126.   And the correct response for true will be returned (1 or -1). Note that
  127.   this also works in the following manner.
  128.  
  129.   PRINT 5=9
  130.   returns a 0
  131.  
  132.   PRINT 90=90
  133.   returns a -1 (or a 1)
  134.  
  135.   etc. Although I can't think of a reason to do this, you might be able to.
  136.  
  137.                                  EXERCISES   
  138.  
  139.    To use these exercises, try to work out what a will equal, and then enter
  140.   the expression into the computer, and print a, to see if you were correct.
  141.  
  142.   All these exercises use the values of these variables:
  143. b=5, c=9, d=15, e=5, f=4, g=2, h=15, i=-1, j=-1, k=-1, l=0, m=0, n=-1, o=0
  144.  
  145. 1)
  146.  
  147.   a=(i=(b=e))
  148.  
  149. 2)
  150.  
  151.   a=(l=(b=f))
  152.  
  153. 3)
  154.  
  155.   a=(g=2*(k=(l=(h=g))))
  156.  
  157. 4)
  158.  
  159.   a=4*ABS(b=e)
  160.  
  161.  
  162.    I hope that the above is of some use to one of you.
  163.  
  164.                                 - Paul Mclachlan
  165.  
  166.  
  167.  
  168.    ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 |||
  169.  
  170.  
  171.